package org.hsieh.tr.explore.dbsource.impl;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

@Deprecated
public class DbMetadataParser {

    public static void main(String[] args) {
	Connection conn = null;

	try {
	    String userName = "root";
	    String password = "";
	    String url = "jdbc:mysql://localhost/mysql";
	    Class.forName("com.mysql.jdbc.Driver").newInstance();
	    conn = DriverManager.getConnection(url, userName, password);
	    System.out.println("Database connection established");

	    DatabaseMetaData meta = conn.getMetaData();
	    ResultSet rs = meta.getTables(null, null, null, null);
	    while (rs.next()) {
		/*
		 * TABLE_CAT -->TABLE_CAT
		 * 
		 * TABLE_SCHEM -->TABLE_SCHEM
		 * 
		 * TABLE_NAME-->TABLE_NAME
		 * 
		 * TABLE_TYPE -->TABLE_TYPE
		 */
		ResultSetMetaData metaData2 = rs.getMetaData();
		for (int i = 1; i < metaData2.getColumnCount(); i++) {
		    System.out.println(metaData2.getColumnName(i) + " -->"
			    + metaData2.getColumnLabel(i));
		}
		System.out.println(rs.getObject(3));
		System.out.println(rs.getObject(4));
	    }
	    // System.out.println(rs.next());

	    // ResultSet tables = meta.getTables(null, null, null, new String[]
	    // {
	    // "TABLE", "VIEW" });
	    // System.out.println(tables.next());
	    // DbMetadataParser parser = new DbMetadataParser(meta);
	    // List<ISchemaEntity> schemas = parser.findSchemaEntities();
	    // System.out.println(schemas.size());
	    // for (ISchemaEntity schem : schemas) {
	    // System.out.println(schem);
	    // }
	    // ISchemaEntity sc = schemas.get(1);
	    // sc.getTables();
	} catch (Exception e) {
	    e.printStackTrace();
	    System.err.println("Cannot connect to database server");
	} finally {
	    if (conn != null) {
		try {
		    conn.close();
		    System.out.println("Database connection terminated");
		} catch (Exception e) { /* ignore close errors */
		}
	    }
	}

    }

    public DbMetadataParser(DatabaseMetaData meta) {
	this.metaData = meta;
    }

    private DatabaseMetaData metaData;

    // String[] types = new String[] { "TABLE", "VIEW" };
    //
    // /**
    // * 1.TABLE_CAT
    // *
    // * 2.TABLE_SCHEM
    // *
    // * 3.TABLE_NAME
    // *
    // * 4.TABLE_TYPE
    // */
    // public static List<ITableEntity> findTables(ISchemaEntity schema)
    // throws SQLException {
    // List<ITableEntity> tables = new ArrayList<ITableEntity>();
    // String[] types = new String[] { "TABLE", "VIEW" };
    // DatabaseMetaData metaData = schema.getMetaData();
    // ResultSet rs = metaData.getTables(schema.getTableCatalog(), schema
    // .getName(), null, types);
    // while (rs.next()) {
    // TableEntityImpl table = new TableEntityImpl();
    // table.setSchema(schema);
    // table.setMetaData(metaData);
    // table.setName(rs.getObject(3).toString());
    // table.setType(rs.getObject(4).toString());
    // tables.add(table);
    // }
    // return tables;
    // }
    //
    // public List<ISchemaEntity> findSchemaEntities() throws SQLException {
    // String term = metaData.getSchemaTerm();
    // System.out.println("Term " + term);
    // ResultSet schemas = metaData.getSchemas();
    // List<ISchemaEntity> res = new ArrayList<ISchemaEntity>();
    // while (schemas.next()) {
    // SchemaEntityImpl sche = new SchemaEntityImpl();
    // sche.setMetaData(metaData);
    // sche.setName(schemas.getString(1));
    // sche.setTableCatalog(schemas.getString(2));
    // sche.setDefault(schemas.getBoolean(3));
    // res.add(sche);
    // }
    // if (res.isEmpty()) {
    // res.add(FakeSchema.create(metaData));
    // }
    // return res;
    // }
}
